package com.app.activitys;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;

import org.apache.http.util.ByteArrayBuffer;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.widget.ImageView;
import android.widget.TextView;

import com.app.entidades.Bar;
import com.app.entidades.Show;
import com.example.applucianokikao.R;

public class ActivityDetalhesShow extends Activity{
	
	private Show show;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_detalhes_show);	
		
		
		Bundle extras = getIntent().getExtras();
		Bundle valor = extras.getBundle("show_bundle");
		show = (Show) valor.getSerializable("show_objeto");
		
		TextView nome = (TextView) findViewById(R.id.txt_nome_show);
		TextView atracoes = (TextView) findViewById(R.id.txt_atracoes_show);
		TextView local = (TextView) findViewById(R.id.txt_local_show);
		TextView data = (TextView) findViewById(R.id.txt_data_show);
		TextView horario = (TextView) findViewById(R.id.txt_horario_show);
		ImageView capa = (ImageView) findViewById(R.id.img_capa_show);
		
		Bitmap m = buscaImagem(nomeImagem(show.getBanner()));
		
		if(m==null){
			baixaImagem(show.getBanner());
			capa.setImageResource(R.drawable.foto_padrao_show);
		}else{
			capa.setImageBitmap(m);
		}
		
		
		nome.setText(show.getNome());
		atracoes.setText(show.getAtracoes());
		local.setText(show.getLocal());
		data.setText(show.getDia());
		horario.setText(show.getHora());
		
	}
	
	private void baixaImagem(final String url) {
	      System.out.println("baixando");
			new Thread(new Runnable() {	
				@Override
				public void run() {
				        try {
				        	URL aURL = new URL(url);
						    URLConnection conn = aURL.openConnection();
						    conn.connect();
						        
						    InputStream is = conn.getInputStream();
						    final BufferedInputStream bis = new BufferedInputStream(is);
						    
						    String nome = nomeImagem(url);
						    
						    File file = new File(Environment.getExternalStorageDirectory()+"/Kikao/");
						    
						    FileOutputStream fos = new FileOutputStream(file+"/"+nome);
						    
						    fos.write(getBytes(bis));
						    fos.flush();
						    fos.close();
						    System.out.println("Baixou e Salvou");
						    bis.close();
						    is.close();
					   } catch (IOException e) {
						      Log.e("ImagemButton", "Error getting bitmap", e);
					   }
				}
	    	}).start();
	    }
		
		private Bitmap buscaImagem(String nomeImagem){
			Bitmap bmp=null;
				
			    File file = new File(Environment.getExternalStorageDirectory()+"/Kikao/"+nomeImagem);
			    if(file.exists()){
			    	System.out.println("existe");
			    	bmp = BitmapFactory.decodeFile(file.getPath());
			    	System.out.println("passou");
			    }else{
			    	System.out.println("não existe");
			    }
				return bmp;
			
		}
		
		private String nomeImagem(String link){
			String[] valores = link.split("/");
			return valores[valores.length-1];
		}
		
		
		public byte[] getBytes(BufferedInputStream bis){
			ByteArrayBuffer baf = new ByteArrayBuffer(50);

			int count = 0;

			try {
				while((count = bis.read()) != -1){
					baf.append((byte) count);
				}
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			return baf.toByteArray();
		}
}
